// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.domain.communiquer;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import fr.upstlse.glre.business.domain.user.User;
import fr.upstlse.glre.business.vo.communiquer.MessageVO;

/**
 * @see fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres
 */
public class BoiteAuxLettresDaoImpl
    extends fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettresDaoBase
{
	
	private static final String BYPROPRIETAIRE_QUERY = 
		"select bal from "
		+ BoiteAuxLettres.class.getName() + " bal"
		+ " left join bal.proprietaire user"
		+ " where user=:proprietaire";
		
    /**
     * @see fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettresDao#toBoiteAuxLettresVO(fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres, fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO)
     */
    public void toBoiteAuxLettresVO(
        fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres source,
        fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO target)
    {
        super.toBoiteAuxLettresVO(source, target);
        target.setProprietaire(getUserDao().toUserVO(source.getProprietaire()));
        if(source.getMessages() != null) {
        	Collection<MessageVO> lMessageVOs = new HashSet<MessageVO>();
        	Collection<Message> lMessages = source.getMessages();
        	for(Message lMessage : lMessages) {
        		lMessageVOs.add(getMessageDao().toMessageVO(lMessage));
        	}
        	target.setMessages(lMessageVOs);
        }
    }


    /**
     * @see fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettresDao#toBoiteAuxLettresVO(fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres)
     */
    public fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO toBoiteAuxLettresVO(final fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres entity)
    {
        return super.toBoiteAuxLettresVO(entity);
    }


    /**
     * Retrieves the entity object that is associated with the specified value object
     * from the object store. If no such entity object exists in the object store,
     * a new, blank entity is created
     */
    private fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres loadBoiteAuxLettresFromBoiteAuxLettresVO(fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO boiteAuxLettresVO)
    {
        fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres boiteAuxLettres = this.load(boiteAuxLettresVO.getId());
        if (boiteAuxLettres == null)
        {
            boiteAuxLettres = fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres.Factory.newInstance();
        }
        return boiteAuxLettres;
    }

    
    /**
     * @see fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettresDao#boiteAuxLettresVOToEntity(fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO)
     */
    public fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres boiteAuxLettresVOToEntity(fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO boiteAuxLettresVO)
    {
        fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres entity = this.loadBoiteAuxLettresFromBoiteAuxLettresVO(boiteAuxLettresVO);
        this.boiteAuxLettresVOToEntity(boiteAuxLettresVO, entity, true);
        return entity;
    }


    /**
     * @see fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettresDao#boiteAuxLettresVOToEntity(fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO, fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres)
     */
    public void boiteAuxLettresVOToEntity(
        fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO source,
        fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres target,
        boolean copyIfNull)
    {
        super.boiteAuxLettresVOToEntity(source, target, copyIfNull);
        target.setId(source.getId());
        if(copyIfNull || source.getProprietaire() != null) {
        	target.setProprietaire(getUserDao().userVOToEntity(source.getProprietaire()));
        }
        if(copyIfNull || source.getMessages() != null) {
        	Collection<MessageVO> lMessageVOs = source.getMessages();
        	Set<Message> lMessages = new HashSet<Message>();
        	if(lMessageVOs != null) {
        		for(MessageVO lMessageVO : lMessageVOs) {
            		lMessages.add(getMessageDao().messageVOToEntity(lMessageVO));
            	}
        	}
        	target.setMessages(lMessages);
        }
    }


	@Override
	public Object findByProprietaire(int pTransform, User pProprietaire) {
		return findByProprietaire(pTransform, BYPROPRIETAIRE_QUERY, pProprietaire);
	}
    
    

}